package com.by.controller;

import com.by.biz.IUserBiz;
import com.by.model.Result;
import com.by.model.User;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    private IUserBiz userDAO;
    @RequestMapping("/tolist")
    public ModelAndView tolist() {
        return new ModelAndView("list");
    }
    @RequestMapping("/getPage")
    public Map queryUserByPage(int pageNum, int pageSize, User user){
        Map map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("user", user);
        Page page=userDAO.queryLikeUsers(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());
        return map;
    }
    @RequestMapping("/add")
    public Result addUser(User user) {
        userDAO.addUser(user);
        Result result = new Result();
        return result;
    }

    @RequestMapping("/delete")
    public Result deleteUser(String id) {

        userDAO.deleteUserbyId(Integer.parseInt(id));
        return  new Result();
    }
    @RequestMapping("/goupdate")
    public ModelAndView goupdate(User user) {
        User User1 =userDAO.getUser(user.getId());
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("User",User1);
        return new ModelAndView("update",map);
    }
    @RequestMapping("/updateController")
    public Result updateUser(User user) {
        userDAO.update(user);
        return  new Result();
    }



}

